// @flow
import React, { Component } from 'react'
import moment from 'moment'
import { AgGridReact } from 'ag-grid-react'
import { TableResize } from 'ag-grid-addons'
import styles from './JobListGrid.css'
import JobListActions from './JobListActions'
import JobStatusActions from './JobStatusActions'
import JobWhereActions from './JobWhereActions'
type Props = {
  rowData: Array<Object>,
  refreshJobList: Function,
  history: Object
}
type States = {
  columnDefs: Array<Object>
}
class JobListGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  TableResize: Function
  api: Object
  constructor (props: Props) {
    super(props)
    this.state = {
      columnDefs:  [
        { headerName: '序号1',
          field: 'key',
          width: 20,
          minWidth: 80,
          maxWidth: 80,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        { headerName: '作业编号',
          field: 'jobId',
          width: 80,
          cellRendererFramework: JobWhereActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        { headerName: '分析任务',
          field: 'taskName',
          width: 150,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        // { headerName: '分析模型',
        //   width: 150,
        //   cellRendererParams: {
        //   },
        //   cellRendererFramework: JobWhereActions,
        //   cellStyle: {
        //     padding: '8px',
        //     'text-align': 'center'
        //   }
        // },
        // { headerName: '分析设备',
        //   field: 'task',
        //   width: 150,
        //   cellStyle: {
        //     padding: '8px',
        //     'text-align': 'center'
        //   },
        //   cellRenderer: (params) => {
        //     let deviceNames = '<div>'
        //     params.data.task.device.map((d, i) => {
        //       if (d.deviceName !== null) {
        //         deviceNames += '[' + d.deviceName + ']'
        //       }
        //     })
        //     deviceNames += '</div>'
        //     return deviceNames
        //   }
        // },
        { headerName: '启动时间',
          field: 'startTime',
          width: 160,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
          // cellRenderer: (params) => {
          //   if (params.data.startTime === null) {
          //     return ''
          //   } else {
          //     return moment.utc(params.data.startTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          //   }
          // }
        },
        { headerName: '结束时间',
          field: 'endTime',
          width: 160,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
          // cellRenderer: (params) => {
          //   if (params.data.endTime === null) {
          //     return ''
          //   } else {
          //     return moment.utc(params.data.endTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          //   }
          // }
        },
        { headerName: '状态',
          field: 'status',
          width: 100,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        {
          headerName: '运行状态',
          width: 120,
          cellRendererParams: {
          },
          cellRendererFramework: JobStatusActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        {
          headerName: '操作',
          minWidth: 200,
          cellRendererParams: {
            refreshJobList: this.props.refreshJobList,
            history: this.props.history
          },
          cellRendererFramework: JobListActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  render () {
    const { columnDefs } = this.state
    const { rowData } = this.props
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData,
          onGridReady: this.onGridReady
        }}
          />
      </div>
    )
  }
}

export default JobListGrid
